home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
print
/
gs261sr1.zip
/
GDEVMEM1.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-26
|
18KB
|
560 lines
/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* gdevmem1.c */
/* Generic and monobit "memory" (stored bitmap) device */
/* for Ghostscript library. */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gxdevice.h"
#include "gxdevmem.h" /* semi-public definitions */
#include "gdevmem.h" /* private definitions */
/* Define masks for little-endian operation. */
const ushort gdev_mem_swapped_left_masks[17] = {
0xffff, 0xff7f, 0xff3f, 0xff1f, 0xff0f, 0xff07, 0xff03, 0xff01,
0xff00, 0x7f00, 0x3f00, 0x1f00, 0x0f00, 0x0700, 0x0300, 0x0100,
0x0000
};
/* ------ Generic code ------ */
/* Return the appropriate memory device for a given */
/* number of bits per pixel (0 if none suitable). */
const gx_device_memory *
gdev_mem_device_for_bits(int bits_per_pixel)
{ switch ( bits_per_pixel )
{
case 1: return &mem_mono_device;
case 2: return &mem_mapped2_color_device;
case 4: return &mem_mapped4_color_device;
case 8: return &mem_mapped8_color_device;
case 16: return &mem_true16_color_device;
case 24: return &mem_true24_color_device;
case 32: return &mem_true32_color_device;
default: return 0;
}
}
/* Compute the size of the bitmap storage, */
/* including the space for the scan line pointer table. */
/* Note that scan lines are padded to a multiple of 4 bytes, */
/* and additional padding may be needed if the pointer table */
/* must be aligned 0 mod 8. */
private ulong
mem_bitmap_bits_size(const gx_device_memory *dev)
{ return round_up((ulong)dev->height * gdev_mem_raster(dev),
max(4, arch_align_long_mod));
}
ulong
gdev_mem_bitmap_size(const gx_device_memory *dev)
{ return mem_bitmap_bits_size(dev) +
(ulong)dev->height * sizeof(byte *);
}
/* Open a memory device, allocating the data area if appropriate, */
/* and create the scan line table. */
int
mem_open(gx_device *dev)
{ byte *scan_line;
uint raster = mdev->raster = gdev_mem_raster(mdev);
byte **pptr;
byte **pend;
if ( mdev->memory_procs != 0 )
{ /* Allocate the data now. */
ulong size = gdev_mem_bitmap_size(mdev);
if ( (uint)size != size )
return gs_error_limitcheck;
mdev->base = (byte *)(*mdev->memory_procs->alloc)(1, (uint)size, "mem_open");
if ( mdev->base == 0 )
return gs_error_VMerror;
}
scan_line = mdev->base;
pptr = (byte **)byte_ptr_add(scan_line, mem_bitmap_bits_size(mdev));
pend = pptr + dev->height;
mdev->line_ptrs = pptr;
while ( pptr < pend )
{ *pptr++ = scan_line;
scan_line = byte_ptr_add(scan_line, raster);
}
return 0;
}
/* Return the initial transformation matrix */
void
mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
{ pmat->xx = mdev->initial_matrix.xx;
pmat->xy = mdev->initial_matrix.xy;
pmat->yx = mdev->initial_matrix.yx;
pmat->yy = mdev->initial_matrix.yy;
pmat->tx = mdev->initial_matrix.tx;
pmat->ty = mdev->initial_matrix.ty;
}
/* Test whether a device is a memory device */
int
gs_device_is_memory(const gx_device *dev)
{ /* We can't just compare the procs, or even an individual proc, */
/* because we might be tracing. Compare the device name, */
/* and hope for the best. */
const char *name = dev->dname;
int i;
for ( i = 0; i < 6; i++ )
if ( name[i] != "image("[i] ) return 0;
return 1;
}
/* Ensure that the data bytes are in big-endian order. */
/* This is no longer needed. */
void
gdev_mem_ensure_byte_order(gx_device_memory *dev)
{
}
/* Close a memory device, freeing the data area if appropriate. */
int
mem_close(gx_device *dev)
{ if ( mdev->memory_procs != 0 )
(*mdev->memory_procs->free)((char *)mdev->base,
1, (uint)gdev_mem_bitmap_size(mdev), "mem_close");
return 0;
}
/* Copy a scan line to a client. */
#undef chunk
#define chunk byte
int
mem_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
{ byte *src;
if ( y < 0 || y >= dev->height )
return gs_error_rangecheck;
src = scan_line_base(mdev, y);
if ( actual_data == 0 )
memcpy(str, src, gx_device_raster(dev, 0));
else
*actual_data = src;
return 0;
}
/* Return the xfont procedure vector. */
gx_xfont_procs *
mem_get_xfont_procs(gx_device *dev)
{ gx_device *target = mdev->target;
return (target == 0 ? gx_default_get_xfont_procs(dev) :
(*target->procs->get_xfont_procs)(target));
}
/* Return the xfont device. */
gx_device *
mem_get_xfont_device(gx_device *dev)
{ gx_device *target = mdev->target;
return (target == 0 ? gx_default_get_xfont_device(dev) :
(*target->procs->get_xfont_device)(target));
}
/* ------ Monochrome ------ */
/* Procedures */
private dev_proc_copy_mono(mem_mono_copy_mono);
private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
/* The device descriptor. */
private gx_device_procs mem_mono_procs =
mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle);
/* The instance is public. */
const gx_device_memory mem_mono_device =
mem_device("image(mono)", 1, mem_mono_procs);
/* Convert x coordinate to byte offset in scan line. */
#define x_to_byte(x) ((x) >> 3)
/* Fill a rectangle with a color. */
#undef chunk
#define chunk mono_chunk
private int
mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
gx_color_index color)
{ uint bit;
chunk right_mask;
byte fill;
declare_scan_ptr(dest);
fit_fill(dev, x, y, w, h);
setup_rect(dest);
#define write_loop(stat)\
{ int line_count = h;\
chunk *ptr = dest;\
do { stat; inc_chunk_ptr(ptr, draster); }\
while ( --line_count );\
}
#define write_partial(msk)\
if ( fill ) write_loop(*ptr |= msk)\
else write_loop(*ptr &= ~msk)
switch ( color )
{
case 0: fill = mdev->invert; break;
case 1: fill = ~mdev->invert; break;
case gx_no_color_index: return 0; /* transparent */
default: return -1; /* invalid */
}
bit = x & chunk_align_bit_mask;
if ( bit + w <= chunk_bits )
{ /*
* Only one word. We have to split following statement
* because of a bug in the Xenix C compiler (it produces
* a signed rather than an unsigned shift if we don't
* split).
*/
set_mono_thin_mask(right_mask, w, bit);
}
else
{ int byte_count;
if ( bit )
{ chunk mask;
set_mono_left_mask(mask, bit);
write_partial(mask);
dest++;
w += bit - chunk_bits;
}
set_mono_right_mask(right_mask, w & chunk_bit_mask);
if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
{ write_loop(memset(ptr, fill, byte_count));
inc_chunk_ptr(dest, byte_count);
}
}
if ( right_mask )
write_partial(right_mask);
return 0;
}
/* Copy a monochrome bitmap. */
/* Fetch a chunk from the source. */
/* The source data are always stored big-endian. */
/* Note that the macros always cast cptr, */
/* so it doesn't matter what the type of cptr is. */
/* cshift = chunk_bits - shift. */
#undef chunk
#if arch_is_big_endian
# define chunk uint
# define cfetch_right(cptr, shift, cshift)\
(cfetch_aligned(cptr) >> shift)
# define cfetch_left(cptr, shift, cshift)\
(cfetch_aligned(cptr) << shift)
/* Fetch a chunk that straddles a chunk boundary. */
# define cfetch2(cptr, cskew, skew)\
(cfetch_left(cptr, cskew, sk